package com.gxkj.projects.myshopx.services.impl;

import com.gxkj.common.exceptions.ValidateException;
import com.gxkj.common.utils.ListPager;
import com.gxkj.common.utils.ValidatorUtil;
import com.gxkj.projects.myshopx.dao.GoodTypeDaoImpl;
import com.gxkj.projects.myshopx.entitys.GoodType;
import com.gxkj.projects.myshopx.entitys.User;
import com.gxkj.projects.myshopx.enums.GoodTypeStates;
import com.gxkj.projects.myshopx.services.GoodsTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {


    private  static Logger LOG = LoggerFactory.getLogger(GoodsTypeServiceImpl.class);

    @Autowired
    private GoodTypeDaoImpl goodTypeDao;

    @Autowired
    private ValidatorUtil validatorUtil;

    public void doDeleteGoodType(User adminUser, String id) {
        GoodType entity = this.getById(id);
        entity.setStates(GoodTypeStates.DEL);
        goodTypeDao.update(entity);
    }


    public ListPager<GoodType> doPageHQL(int pagenNo, int pageSize, GoodType entity) {
        return goodTypeDao.doPageHQL(pagenNo,pageSize,entity);
    }


    public void doAddGoodType(User user, GoodType entity) throws ValidateException {
        entity.setCreatedAt(new Date());
        entity.setUpdatedAt(new Date());
        entity.setStates(GoodTypeStates.ENABLE);
        validatorUtil.validate(entity,true);

        goodTypeDao.insert(entity);
    }


    public void doUpdateGoodType(User user, GoodType entity) throws ValidateException {
        GoodType dbEntity = this.getById(entity.getId());
        entity.setCreatedAt(dbEntity.getCreatedAt());
        entity.setStates(dbEntity.getStates());
        entity.setUpdatedAt(new Date());
        validatorUtil.validate(entity,true);

        goodTypeDao.updateByMerge(entity);

    }


    public GoodType getById(String id) {
        return goodTypeDao.selectById(id,GoodType.class);
    }


    public void doEnabledGoodType(User user, String id) throws ValidateException {
        GoodType dbEntity = this.getById(id);
        dbEntity.setUpdatedAt(new Date());
        dbEntity.setStates(GoodTypeStates.ENABLE);
        goodTypeDao.update(dbEntity);
    }


    public void doDisEnabledGoodType(User user, String id) throws ValidateException {
        GoodType dbEntity = this.getById(id);
        dbEntity.setUpdatedAt(new Date());
        dbEntity.setStates(GoodTypeStates.DISABLED);
        goodTypeDao.update(dbEntity);
    }
}
